package com.qboot.generate.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qboot.common.consts.CommonConst;
import com.qboot.common.controller.BaseController;
import com.qboot.common.entity.Result;
import com.qboot.common.entity.TableResult;
import com.qboot.generate.entity.GenTableColumn;
import com.qboot.generate.proterties.GenerateProperties;
import com.qboot.generate.service.GenTableColumnService;
import com.qboot.generate.service.GenTableService;
import com.qboot.generate.utils.VelocityUtils;
import com.qboot.generate.vo.GenTableVo;
import com.qboot.generate.vo.GeneratePathVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代码生成-表
 *
 * @author Tellsea
 * @date 2022-11-25
 */
@Api(tags = "代码生成-表")
@RestController
@RequestMapping("/generate/genTable")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class GenTableController extends BaseController {

    private final GenTableService genTableService;
    private final GenTableColumnService genTableColumnService;
    private final GenerateProperties generateProperties;

    @ApiOperation("查询列表")
    @SaCheckPermission("generate:genTable:list")
    @GetMapping("/list")
    public TableResult<GenTableVo> queryList(GenTableVo entity) {
        return TableResult.ok(genTableService.queryList(getPage(), entity));
    }

    @ApiOperation("查询数据库所有表")
    @GetMapping("/listByDataBase")
    public TableResult listByDataBase(GenTableVo entity) {
        return TableResult.ok(genTableService.listByDataBase(getPage(), entity));
    }

    @ApiOperation("默认生成配置")
    @GetMapping("/getDefaultConfig")
    public Result getDefaultConfig() {
        return queryResult(generateProperties);
    }

    @ApiOperation("根据主键查询")
    @SaCheckPermission("generate:genTable:get")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Long id) {
        return queryResult(genTableService.queryById(id));
    }

    @ApiOperation("新增")
    @SaCheckPermission("generate:genTable:save")
    @PostMapping("/save")
    public Result save(@RequestBody GenTableVo entity) {
        return saveResult(genTableService.saveEntity(entity));
    }

    @ApiOperation("根据主键更新")
    @SaCheckPermission("generate:genTable:update")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody GenTableVo entity) {
        return updateResult(genTableService.updateEntity(entity));
    }

    @ApiOperation("根据主键删除")
    @SaCheckPermission("generate:genTable:remove")
    @GetMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        boolean removeFlag = genTableService.removeById(id);
        boolean removeBatchFlag = genTableColumnService.remove(new LambdaQueryWrapper<GenTableColumn>()
                .eq(GenTableColumn::getTableId, id));
        return removeResult(removeFlag && removeBatchFlag);
    }

    @ApiOperation("根据主键批量删除")
    @SaCheckPermission("system:sysDict:removeBatch")
    @GetMapping("/removeBatchByIds/{ids}")
    public Result removeBatchByIds(@PathVariable("ids") List<Long> ids) {
        boolean removeFlag = genTableService.removeBatchByIds(ids);
        for (Long id : ids) {
            boolean removeBatchFlag = genTableColumnService.remove(new LambdaQueryWrapper<GenTableColumn>()
                    .eq(GenTableColumn::getTableId, id));
            removeFlag = removeFlag && removeBatchFlag;
        }
        return removeResult(removeFlag);
    }

    @ApiOperation("代码预览")
    @SaCheckPermission("generate:genTable:preview")
    @GetMapping("/preview/{id}")
    public Result preview(@PathVariable("id") Long id) {
        GeneratePathVo config = new GeneratePathVo();
        config.setType(0);
        return Result.ok("生成成功", VelocityUtils.generate(id, config));
    }

    @ApiOperation("获取生成路径默认配置")
    @GetMapping("/getPathConfig")
    public Result getPathConfig() {
        GeneratePathVo generatePathVo = new GeneratePathVo();
        generatePathVo.setType(1);
        generatePathVo.setDefaultPath(StrUtil.replace(CommonConst.GENERATE_PATH, "\\", "/"));
        generatePathVo.setServicePath(StrUtil.replace(System.getProperty("user.dir"), "\\", "/"));
        generatePathVo.setPcPath(StrUtil.replace(StrUtil.replaceLast(generatePathVo.getServicePath(), "qboot", "qboot-vue3"), "\\", "/"));
        return Result.ok("获取成功", generatePathVo);
    }

    @ApiOperation("代码生成")
    @SaCheckPermission("generate:genTable:generate")
    @PostMapping("/generate")
    public Result generate(@RequestBody GeneratePathVo entity) {
        VelocityUtils.generate(entity);
        return Result.ok("生成成功");
    }
}
